<?php

namespace app\admin\controller;

use think\Db;
use think\Session;
use think\Model;
header("Content-type:text/html;charset=utf-8");

/*
 * 增值服务管理
 */

class Appreciate extends Base
{

    /*
     * 增值列表
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize = input('limit') ? input('limit') : $conf['paginate']['list_rows'];

        $list = Db('appreciate')->where('is_del',0)->order("id desc")->paginate($pageSize);
        $this->assign('total', $list->total());
        $this->assign('page', $list->render());
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加增值
     * wangtiantian
     */
    public function add()
    {
        if (request()->isAjax()) {

            $primary_menu = input('primary_menu');
            $price = input('price');
            $period_validity = input('period_validity');

            $data['sub_menu'] = input('arr_sub');
            $data['sub_menu'] = json_decode($data['sub_menu'], true);

            foreach ($data['sub_menu'] as $k=>$v){
                $arr = explode('&', $v);
                $data['sub_menu'][$k] = $arr;
            }

            $data = [
                'primary_menu' => $primary_menu,
                'price'        => $price,
                'period_validity' => $period_validity,
                'sub_menu' => json_encode($data['sub_menu'],JSON_UNESCAPED_UNICODE),
                'create_time'  => time()
            ];

            $res = Db('appreciate')->insert($data);
            if ($res){
                $this->success("添加成功");
            }else{
                $this->error("网络异常");
            }

        }

        return $this->fetch();
    }



    /**
     * 编辑增值
     */
    public function edit()
    {
        $id = input('id');
        // 更新数据
        if (request()->isPost()) {

            $primary_menu = input('primary_menu');
            $price = input('price');
            $period_validity = input('period_validity');
            $id = input('id');

            $data['sub_menu'] = input('arr_sub');
            $data['sub_menu'] = json_decode($data['sub_menu'], true);

            foreach ($data['sub_menu'] as $k=>$v){
                $arr = explode('&', $v);
                $data['sub_menu'][$k] = $arr;
            }

            $data = [
                'primary_menu' => $primary_menu,
                'price'        => $price,
                'period_validity' => $period_validity,
                'sub_menu' => json_encode($data['sub_menu'],JSON_UNESCAPED_UNICODE)
            ];

            $ret = Db('appreciate')->where('id', $id)->update($data);
            if ($ret) {
                $this->success("编辑成功");
            } else {
                $this->error("网络异常");
            }

        }
        // 详情
        $info = Db('appreciate')->where('id', $id)->find();
        $sub_menu = $info['sub_menu'];
        $info['sub_menu'] = json_decode($sub_menu);
        $count = count($info['sub_menu']);
        $this->assign([
            'info'  => $info,
            'count' => $count
        ]);
        $this->assign('info', $info);
        $this->assign('count', $count);
        return $this->fetch();
    }

    /*
     * 启用或禁用
     */
    public function is_show()
    {

        if (request()->isPost()) {

            $id     = input('id');
            $status = input('status');

            $res = Db('appreciate')->where('id', $id)->update(['status' => $status]);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }

    /**
     * 软删除
     */
    public function del()
    {
        if (request()->isPost()) {
            $id = input('id');
            $res = Db('appreciate')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }

}

?>